<template>
  <div style="height:100%">
    <link rel="stylesheet" type="text/css" href="./iconfont.css">
    <el-container class="home-container">
      <!-- 头部 -->
      <el-header height="100px">
        <div>
          <img src="../assets/heima.png" alt="">
          <span> 后台管理系统 </span>
        </div>
        <el-button type="info" @click="logout">退出</el-button>
      </el-header>
      <!-- 页面主体 -->
      <el-container height="100%">
        <!-- 侧边栏 -->
        <el-aside :width="isCollapseL ? '70px' : '300px'">
          <!-- 菜单折叠栏 -->
          <div class="toggle-button" @click="toggleCollapse">
            {{ collapseText }}
          </div>
          <el-menu
            class="el-menu-vertical-demo"
            background-color="#373d41"
            active-text-color="#409eff"
            text-color="#fff"
            :default-active="activePath"
            :unique-opened="true"
            :collapse="isCollapseL"
            :collapse-transition="false"
            :router="true"
            @open="handleOpen"
            @close="handleClose"
          >
            <!-- 一级菜单 -->
            <el-submenu
              v-for="item in menuList"
              :key="item.id"
              :index="item.id + ''"
            >
              <!-- 一级菜单模板区 -->
              <template slot="title">
                <!-- 菜单图标 -->
                <i :class="iconsListObj[item.id]" />
                <!-- 菜单名称 -->
                <span>{{ item.authName }}</span>
              </template>

              <!-- 二级菜单 -->
              <el-MenuItem
                v-for="subItem in item.children"
                :key="subItem.id"
                :index="'/' + subItem.path"
                @click="saveNavState('/' + subItem.path)"
              >
                <!-- 二级菜单模板区 -->
                <template slot="title">
                  <!-- 菜单图标 -->
                  <i class="el-icon-menu" />
                  <!-- 菜单名称 -->
                  <span>{{ subItem.authName }}</span>
                </template>
              </el-MenuItem>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 右侧内容 -->
        <el-main>
          <!-- 路由占位符 -->
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menuList: [],
      iconsListObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-showpassword',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-tijikongjian'
      },
      // 是菜单否折叠
      isCollapseL: false,
      collapseText: '<<<<=',
      // 当前激活的链接地址
      activePath: ''
    }
  },

  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },

  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有的菜单
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    },
    // 点击按钮切换菜单折叠和展开
    toggleCollapse() {
      this.isCollapseL = !this.isCollapseL
      if (this.isCollapseL) {
        this.collapseText = '=>>>'
      } else {
        this.collapseText = '<<<='
      }
    },
    // 保存链接的激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    },
    handleOpen(key, keyPath) {
      console.log(key, keyPath)
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath)
    }

  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;

  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.toggle-button {
  background-color: #333744;
  font-size: 15px;
  line-height: 30px;
  color: #fff;
  text-align: center;
  // letter-spacing: 0.1em;
  cursor: pointer;
  // padding-left: 10px;
}
.el-submenu {
  font-size: 40;
}
.el-main {
  background-color: #eaedf1;
}

.iconfont {
  margin-right: 10px;
  font-size: 30px;
}
</style>
